{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "# ChemSpiPy: Getting Started\n",
    "\n",
    "Before we start:\n",
    "\n",
    "- Make sure you have [installed ChemSpiPy](http://chemspipy.readthedocs.io/en/latest/guide/install.html#install).\n",
    "- [Obtain a security token](http://chemspipy.readthedocs.io/en/latest/guide/intro.html#securitytoken) from the ChemSpider web site.\n",
    "\n",
    "## First Steps\n",
    "\n",
    "Start by importing ChemSpider:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "from chemspipy import ChemSpider"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "Then connect to ChemSpider by creating a ChemSpider instance using your security token:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "# Tip: Store your security token as an environment variable to reduce the chance of accidentally sharing it\n",
    "import os\n",
    "mytoken = os.environ['CHEMSPIDER_SECURITY_TOKEN']\n",
    "\n",
    "cs = ChemSpider(security_token=mytoken)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "All your interaction with the ChemSpider database should now happen through this ChemSpider object, `cs`.\n",
    "\n",
    "## Retrieve a Compound\n",
    "\n",
    "Retrieving information about a specific Compound in the ChemSpider database is simple.\n",
    "\n",
    "Let’s get the Compound with [ChemSpider ID 2157](http://www.chemspider.com/Chemical-Structure.2157.html):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAYAAAA8AXHiAAAAAXNSR0IArs4c6QAAAARnQU1BAACx\njwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAACo1JREFU\neF7tnTGIVD0Qx/0EwU4RBEsbwcLCwkLEQrFQO68QxEq8QkFQwQPtBEEsLS1ULLUTC7G0tBJLsRIU\ntLRQEBRc95919svt5Xbf22QyeTMTWE6z702Smd/m5U0yyX+jcdriyTVQWgMAy5NroLQGtpQW6PJc\nA+Ep6GpwDXBowMHi0KrL9B7LGeDRgPdYPHo1L9XBMo8AjwIcLB69mpfqYJlHgEcBDhaPXs1LdbDM\nI8CjAAeLR6/mpTpY5hHgUYCDxaNX81IdLPMI8CjAweLRq3mpDpZ5BHgU4GDx6NW8VAfLPAI8CnCw\nePRqXqqDZR4BHgU4WDx6NS/VwTKPAI8CHCwevZqX6mCZR4BHAQ4Wj17NS3WwzCPAowAHi0ev5qU6\nWOYR4FGAg8WjV/NSHSzzCPAowMHi0at5qQ6WeQR4FOBg8ejVvFQTYP38+XO0uro6unnz5uj27dtF\nPzdu3AiyPa3XgAmwrl69in1WWT9ra2vOVqQB9WB9/fp1tH379tG2bdtG165dK9pbofe7dOlSAHbP\nnj2jb9++OVz/NKAerAsXLgTDX79+nc3oly9fZi+DrfJMglWD9e7du2lvgnEWV6JeET3j+/fvuYoZ\nlFzVYB08eDCA9eTJk6lRAFsJyPDYiyG6f/9+KOvMmTODAoCrsmrBevr0aTA04KIEwJB37969bH3i\n0QpZr1+/DrIA6969e0Pe8+fPs+UPXYBKsGBkDKZh5Ddv3kwNT3notXJTClwARTCX6BVz6yh5v0qw\nbt26teGxRHnnzp0rpu/Dhw+Hch48eDCVeerUqZCHR6Plp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Bi90MckEnTHIDMapIIolUFVjylEYP0588fq0xN24235keP\nHo1+/fpVRReqwILGKFLl8+fPEwVWmkqpYq2MQn7//v1PHath7RY+R8dzmC9evFgnlb6bLWqz/M2q\npA4sNPTt27eT9laa/M2wd9VbARJtawlQaP1XDJeDtcgkFdaaL6pCS9/fHesDIAGuL1++THsvyqO6\nOliLrNZxgd0iMVq+p97qAx0eMG4YAFtZWVl3fIuDtcjiHZYELxKh6fuuYyR6RNL19JceoV11onKM\nFRrvYHUalKcG6fE4zMHaqKHJUR6V9jXv+kuWuo56In8U5lqg4lrz3KrWuH9tba3o4D2OKErVX++j\n0N0N6+z9/fv34Lsq4W6AjDjN/h/f6QYLLXQH6ZQBwLU61kdpB6lNsGo8Z4yWQY9De49Cowav3Wzv\nsWprXGl5PsZSatgWmmX7rbAFCxitg/63QqOGlW62gyVtAaXlO1hKDSvdLAdL2gJKy3ewlBpWulkO\nlrQFlJbvYCk1rHSzHCxpCygt38FSaljpZjlY0hZQWr6DpdSw0s1ysKQtoLR8B0upYaWb5WBJW0Bp\n+Q6WUsNKN8vBkraA0vIdLKWGlW6WgyVtAaXlO1hKDSvdLAdL2gJKy3ewlBpWulkOlrQFlJbvYCk1\nrHSzHCxpCygt38FSaljpZv0FqtR95byLG1IAAAAASUVORK5CYII=\n",
      "text/plain": [
       "Compound(2157)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comp = cs.get_compound(2157)\n",
    "comp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "Now we have a [Compound](http://chemspipy.readthedocs.org/en/latest/api.html#chemspipy.Compound) object called `comp`. We can get various identifiers and calculated properties from this object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C_{9}H_{8}O_{4}\n",
      "180.1574\n",
      "CC(=O)Oc1ccccc1C(=O)O\n",
      "Aspirin\n"
     ]
    }
   ],
   "source": [
    "print(comp.molecular_formula)\n",
    "print(comp.molecular_weight)\n",
    "print(comp.smiles)\n",
    "print(comp.common_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "## Search for a name\n",
    "\n",
    "What if you don’t know the ChemSpider ID of the Compound you want? Instead use the `search` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound(5589)\n",
      "Compound(58238)\n",
      "Compound(71358)\n",
      "Compound(96749)\n",
      "Compound(2006622)\n",
      "Compound(5341883)\n",
      "Compound(5360239)\n",
      "Compound(9129332)\n",
      "Compound(9281077)\n",
      "Compound(9312824)\n",
      "Compound(9484839)\n",
      "Compound(9655623)\n"
     ]
    }
   ],
   "source": [
    "for result in cs.search('glucose'):\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "The search method accepts any identifer that ChemSpider can interpret, including names, registry numbers, SMILES and InChI."
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
